﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using digit.Core;
using SMS.ServiceManager;
using SMS.UserManager;

public partial class Modules_ListPlayer : WebControlBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.lnkDelete.Attributes.Add("onclick", "return confirm('Bạn đã chắc chắn xóa?');");
        this.lnkDeleteBottom.Attributes.Add("onclick", "return confirm('Bạn đã chắc chắn xóa?');");

        if (ClientState.MemberInfo.UserType != 0 && ClientState.MemberInfo.UserType != 1)
        {
            lnkAdd.Enabled = false;
            lnkAddBottom.Enabled = false;
            lnkDelete.Enabled = false;
            lnkDeleteBottom.Enabled = false;
        }
        else
        {
            lnkAdd.Enabled = true;
            lnkAddBottom.Enabled = true;
            lnkDelete.Enabled = true;
            lnkDeleteBottom.Enabled = true;
        }

        if (this.IsPostBack) return;

        this.BindData();
    }

    protected void lnkAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("default.aspx?module=DetailPlayer");
    }

    protected void lnkDelete_Click(object sender, EventArgs e)
    {
        int iDeleted = 0;

        //Search all checked rows to delete
        foreach (GridViewRow r in this.grvPlayer.Rows)
        {
            //If checkbox is not checked then quit
            HtmlInputCheckBox cbx = (HtmlInputCheckBox)r.Cells[0].Controls[1];
            if (!cbx.Checked) continue;

            //If checkbox is checked then delete
            if (PlayerManager.Delete(Util.ToInteger(cbx.Value)))
                iDeleted++;
        }

        //Display result of delete process
        if (iDeleted == 0)
        {
            this.lblError.Text = "Không có bản ghi nào được xóa!";
            return;
        }

        this.lblError.Text = "Đã xóa " + iDeleted.ToString() + " bản ghi!";

        this.BindData();
    }

    protected void lnkApprove_Click(object sender, EventArgs e)
    {
        int iApprove = 0;

        //Search all checked rows to approve
        foreach (GridViewRow r in this.grvPlayer.Rows)
        {
            //If checkbox is not checked then quit
            HtmlInputCheckBox cbx = (HtmlInputCheckBox)r.Cells[0].Controls[1];
            if (!cbx.Checked) continue;

            //If checkbox is checked then approve
            if (PlayerManager.UpdateStatus(Util.ToInteger(cbx.Value), 1))
                iApprove++;
        }

        //Display result of approve process
        if (iApprove == 0)
        {
            this.lblError.Text = "Không có bản ghi nào được duyệt!";
            return;
        }

        this.lblError.Text = "Đã duyệt " + iApprove.ToString() + " bản ghi!";

        this.BindData();
    }

    protected void grvPlayer_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.grvPlayer.PageIndex = e.NewPageIndex;
        this.BindData();
    }

    private void BindData()
    {
        string strFilter = "";
        DataView dv = PlayerManager.SelectAll().DefaultView;

        if (this.txtPlayerName.Text != "") strFilter += "PlayerName LIKE '%" + this.txtPlayerName.Text + "%'";
        dv.RowFilter = strFilter;
        dv.Sort = "CreatedDate DESC";

        this.grvPlayer.DataSource = dv;
        this.grvPlayer.DataBind();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        this.BindData();
    }
}
